/**
 * 
 */
package br.embedded.socialapp.business;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;

import br.embedded.socialapp.domain.User;
import br.embedded.socialapp.domain.Usuario;
import br.embedded.socialapp.exceptions.UserException;
import br.embedded.socialapp.location.Coordenates;
import br.embedded.socialapp.location.LocationManager;

/**
 * @author daniel
 */

/**
 * Implements a Web Service representing Infrastructure services API
 */
@WebService
@SOAPBinding(parameterStyle=ParameterStyle.WRAPPED)
public class ProfileServiceWS
{
	
	/**
	 * Adds a new contact to the user's buddy list
	 * @usrLogin1 source user login
	 * @usrLogin2 target user login
	 * @socialNetwork social network
	 * @return true if service was performed with success
	 */
	@WebMethod
	public boolean addFriendship(@WebParam(name="usrLogin1") String usrLogin1, @WebParam(name="usrLogin2") String userLogin2, @WebParam(name="socialNetwork") String socialNetwork)
	{
		return true;
	}
	
	/**
	 * Adds a new user to infrastructure
	 * @param usrLogin user login
	 * @param firstName user first name
	 * @param lastName user last name
	 * @param passwd user password
	 * @return true if service was performed with success
	 */
	@WebMethod
	public boolean registerUser(@WebParam(name="usrLogin") String usrLogin, @WebParam(name="firstName") String firstName, @WebParam(name="lastName") String lastName, @WebParam(name="passwd") String passwd)
	{
		boolean result = false;
		try
		{
			UserManager.getInstance().createUser(usrLogin, firstName, lastName, passwd);
			result = true;
		}
		catch (UserException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * Performs user authentication in infrastructure
	 * @param login user login
	 * @param pwd user password
	 * @return an object representing user
	 */
	@WebMethod
	public Usuario doLogin(@WebParam(name="login") String login, @WebParam(name="pwd") String pwd)
	{
		if (UserManager.getInstance().doLogin(login, pwd))
		{
			try {
				return UserManager.getInstance().getUserByLogin(login);
			} catch (UserException e) {
			}
		}
		return null;
	}
	
	/**
	 * Fetch user personal information
	 * @param userLogin user identifier
	 * @param socialNetwork social network
	 * @return an object representing user
	 */
	@WebMethod
	public User getUserInfo(@WebParam(name="userLogin") String userLogin, @WebParam(name="socialNetwork") String socialNetwork)
	{
		return FriendManager.getInstance().fetchPerson(userLogin, socialNetwork);
	}
	
	/**
	 * Fetch user preferences
	 * @param socialNetworkId OpenSocial user identifier
	 * @param socialNetwork social network
	 * @return list with user preferences
	 */
	@WebMethod
	public ArrayList<String> getUserPreferences(@WebParam(name="userLogin") String userLogin, @WebParam(name="socialNetwork") String socialNetwork)
	{
		return new ArrayList<String>();
	}
	
	/**
	 * Fetches a list of user's friend from specified online social network
	 * @param userLogin OpenSocial user identifier
	 * @param socialNetwork social network
	 * @param detailed indicates if user friends information should be returned detailed
	 * @return list of user's friends
	 */
	@WebMethod
	public ArrayList<User> getFriendsFrom(@WebParam(name="userLogin") String userLogin, @WebParam(name="network") String socialNetwork, @WebParam(name="detailed") boolean detailed)
	{
		/*ArrayList<SimpleProfileInfo> friends = new ArrayList<SimpleProfileInfo>();
		
		SimpleProfileInfo f1 = new SimpleProfileInfo();
		f1.setAboutMe("descrição sobre o Usuário1");
		f1.setId("05504131713099562528");
		f1.setGivenName("Usuario1");
		f1.setFamilyName("Fulano");
		f1.setPhoto("photo.png");
		f1.setUrlProfile("url");
		
		for (int i = 0; i < 10; i++)
			friends.add(f1);
		
		return friends;*/
		ArrayList<User> l = FriendManager.getInstance().fetchFriends(userLogin, socialNetwork, detailed);
		
		System.out.println(socialNetwork + " --> " + l);
		return l;
	}
	
	/**
	 * Lists members by Mobile Social Networks
	 * @param socialNetwork social network
	 */
	@WebMethod
	public ArrayList<User> getMembersFrom(@WebParam(name="socialNetwork") String socialNetwork)
	{
		return new ArrayList<User>();
	}
	
	/**
	 * List the social networks that the user belongs to
	 * @param userLogin user identifier
	 * @return the social networks that the user belongs
	 */
	@WebMethod
	public ArrayList<String> getSocialNetworksFrom(@WebParam(name="userLogin") String userLogin)
	{
		return null;
	}
	
	/**
	 * Requests user's friends list to the specified level
	 * @param userLogin user identifier
	 * @param friendshipLevel friendship level
	 * @return the user's friends list to the specified level
	 */
	@WebMethod
	public ArrayList<User> getFriendsAtLevel(@WebParam(name="userLogin") String userLogin, @WebParam(name="friendshipLevel") int friendshipLevel)
	{
		return null;
	}
	
	/**
	 * Updates user current location
	 * @param login user login in infrastructure
	 * @param latitude latitude coordinate in floating point
	 * @param longitude longitude coordinate in floating point
	 */
	@WebMethod
	public void registerLocation(@WebParam(name="userLogin") String login, 
			@WebParam(name="latitude") double latitude, @WebParam(name="longitude") double longitude)
	{
		LocationManager.getInstance().registerLocation(login, latitude, longitude);
	}
	
	
	/**
	 * Retrieves information about the last user location
	 * @param login user login in infrastructure
	 * @return last user location
	 */
	@WebMethod
	public Coordenates getCurrentLocation(@WebParam(name="userLogin") String login)
	{
		return null;
	}
	
	/**
	 * Requests nearby users
	 * @param coordenate an coordinate object with latitude and longitude informations set
	 * @param date1 end date
	 * @param secondstoSubtract seconds to subtract from end date
	 * @param maxDistance maximal radius from user
	 * @return
	 */
	@WebMethod
	public ArrayList<Coordenates> getNearByUsers(@WebParam(name="coordenate") Coordenates coordenate, 
			@WebParam(name="date1") long date1, @WebParam(name="seconds") int secondstoSubtract, 
			@WebParam(name="maxDistance") double maxDistance)
	{
		
		Calendar calendar = new GregorianCalendar();
		calendar.setTimeInMillis(date1);
		calendar.add(Calendar.SECOND, secondstoSubtract);
		long date2 = calendar.getTimeInMillis();
		
		ArrayList<Coordenates> l = (ArrayList<Coordenates>) LocationManager.getInstance().getNearestUsers(coordenate, new Date(date2), new Date(date1), maxDistance);
		
		return l;
	}
}